﻿@using System.Globalization
@using LETS.Helpers
@using LETS.Models
@using Orchard.ContentManagement
@model LETS.ViewModels.MemberTransactionsViewModel

@{
    var letsSettings = WorkContext.CurrentSite.As<LETSSettingsPart>();
    var minTransactionDay = letsSettings.OldestRecordableTransactionDays * -1;
    var cultureInfo = CultureInfo.GetCultureInfo(WorkContext.CurrentCulture);
    var longDateFormat = cultureInfo.DateTimeFormat.LongDatePattern;
    var oldestCreditValueTransactionDate = string.Empty;
    var creditValuesInSync = true;
    if (Model.Balance > 0) {
        if (Model.OldestCreditValueTransaction != null)
        {
            oldestCreditValueTransactionDate = ((DateTime) Model.OldestCreditValueTransaction.TransactionDate).ToString(longDateFormat);
        }
        else
        {
            creditValuesInSync = false;
        }
    }
}
<header class="page-header">
    <h1>@T("Record a trade")</h1>
</header>
@using (Html.BeginFormAntiForgeryPost())
{
    <table class="table table-striped table-bordered ">
        <thead>
            <tr>
                <th scope="col">@T("Trade date")</th>
                <th scope="col">@T("Member")</th>
                <th scope="col">@T("Notice")</th>
                <th scope="col">@T("Description")</th>
                <th scope="col">@T("Value")</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>
                    @Html.TextBoxFor(m => m.NewTransaction.TransactionDate, new { @class = "required input-small date" })
                    @Html.ValidationMessageFor(m => m.NewTransaction.TransactionDate, @T("The trade date is required").ToString(), new { @class = "help-block" })
                </td>
                <td>
                    @Html.DropDownGroupListFor(model => model.NewTransaction.IdBuyer, Model.NewTransaction.Members, @T("Choose a buyer").ToString())
                    @Html.ValidationMessageFor(model => model.NewTransaction.IdBuyer, @T("The buyer is required").ToString(), new { @class = "help-block" })
                </td>
                <td></td>
                <td>
                    @Html.TextBoxFor(model => model.NewTransaction.Description, new { @class = "required input-xlarge" })
                    @Html.ValidationMessageFor(model => model.NewTransaction.Description, @T("A description is required").ToString(), new { @class = "help-block" })
                </td>
                <td>
                    <div class="input-prepend">
                        <span class="add-on">@Model.NewTransaction.CurrencyUnit</span>@Html.TextBoxFor(model => model.NewTransaction.Value, new { @class = "input-mini" })
                    </div>
                    @Html.ValidationMessageFor(model => model.NewTransaction.Value, @T("The value is required").ToString(), new { @class = "help-block" })
                </td>
            </tr>
        </tbody>
    </table>
    @Html.HiddenFor(model => model.NewTransaction.IdSeller)
    <div class="form-actions">
        <button class="btn btn-primary btn-large" type="submit">@T("Record Trade")</button>
    </div>
}
@using (Script.Foot())
{
    @* generates the localization script *@
    @Display.DatePickerLocalization()
    <script type="text/javascript">    
    //<![CDATA[
        $(function () {
            var txtTransactionDate = $('#@Html.FieldIdFor(m => m.NewTransaction.TransactionDate)');
            txtTransactionDate.datepicker({ showAnim: "", minDate: @minTransactionDay, maxDate: 0, showOn: "both", buttonImageOnly: false });
        })
    //]]>
</script>
}
<header class="page-header">
    <h1>@T("Your trading account")</h1>
</header>
@if(Model.Balance > 0 && letsSettings.UseDemurrage) {
    if (creditValuesInSync)
    {
        <p>
            <strong>@T("Remember, you should spend your credits within {0} days of earning them", letsSettings.DemurrageTimeIntervalDays)</strong><br />
            @Html.ActionLink(T("Here is your demurrage forecast (how much you need to spend & how much time you have)").ToString(), "Demurrage", new { area="LETS", controller="Transactions"}) <br />
        </p>
    }
    else
    {
        <p>@T("The credit values on your account are out of sync.  Please contact admin so that it can be corrected.")</p>
    }
}
@* ReSharper disable Mvc.PartialViewNotResolved *@
@Html.Partial("Transactions")
@* ReSharper restore Mvc.PartialViewNotResolved *@